package com.xls.commonlibrary.db;

import com.xls.commonlibrary.app.MainApplication;
import com.xls.commonlibrary.db.bean.MyObjectBox;

import io.objectbox.BoxStore;


public class ObjectBoxUtil {

    private BoxStore boxStore = null;
    private static volatile ObjectBoxUtil singleton = null;

    private ObjectBoxUtil() {

    }

    // 都用这个实例
    public BoxStore getBoxStore() {
        return boxStore;
    }

    public static ObjectBoxUtil getInstance() {
        if (singleton == null) {
            synchronized (ObjectBoxUtil.class) {
                if (singleton == null) {
                    singleton = new ObjectBoxUtil();
                }
            }
        }
        return singleton;
    }

    public void init() {
        // MyObjectBox todo 找不到就先注释掉，db  bean build一下就有了
        boxStore = MyObjectBox.builder()
                .androidContext(MainApplication.getAppContext())
                .build();
    }

    // https://docs.objectbox.io/troubleshooting
    // https://blog.csdn.net/HuangLin_Developer/article/details/81872884

//    DbSchemaException: DB's last ID is higher
//    Creating a Store throws a DbSchemaException and a message like
//    DB's last entity ID is higher than from model
//    DB's last property ID is higher than the incoming one
//    DB's last index ID is higher than from model
//    DB's last relation ID is higher than from model

    // 旧的表不能正常update到新表导致的，需要处理一下update db的问题 fixme
    // objectbox官方意思是删除app的本地缓存，重新从服务器缓存，感觉太粗暴

}
